//控制层 
app.controller('userController', function($scope, $controller, userService,$location) {

	$controller('baseController', {
		$scope: $scope
	}); //继承
	
	 $scope.showFlag = false;
	 $scope.reloadList=function(){
	    	//切换页码  
	    	$scope.findPage( $scope.paginationConf.currentPage, $scope.paginationConf.itemsPerPage);
	}
	//读取列表数据绑定到表单中  
	$scope.findAll = function() {
		userService.findAll().success(
			function(response) {
				$scope.list = response;
			}
		);
	}
	//分页
	$scope.findPage = function(page, rows) {
		userService.findPage(page, rows).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
				console.log("公司实体-------------"+$scope.list);
			}
		);
	}

	//查询实体 
	$scope.findOne = function(id) {
		userService.findOne(id).success(
			function(response) {
				console.info("公司实体-------------"+response.code);
				$scope.entity = response;
			}
		);
	}

	//保存 
	$scope.save = function() {
		var serviceObject; //服务层对象  	
		console.info("保存的实体是:"+$scope.entity);
		console.info("id:"+$scope.entity.id);
		if($scope.entity.id != null) { //如果有ID
			serviceObject = userService.update($scope.entity); //修改  
		} else {
			serviceObject = userService.add($scope.entity); //增加 
		}
		serviceObject.success(
			function(response) {
				if(response.success) {
					alert(response.message);
					window.location = "user-list.html";
					$scope.reloadList();
					$scope.hideClick();
				} else {
					alert(response.message);
				}
			}
		);
	}
	$scope.hideClick= function(){
		$(".layui-layer-close1").click();
	}
	//批量删除 
	$scope.dele = function() {
		if(confirm('确定要删除这写数据吗?')) {
			//获取选中的复选框			
			userService.dele($scope.selectIds).success(
				function(response) {
					if(response.success) {
						alert(response.message);
						$scope.reloadList(); //刷新列表
						$scope.selectIds = [];
					}
				}
			);
		}
	}

	$scope.searchEntity = {}; //定义搜索对象 

	//搜索
	$scope.search = function(page, rows) {
		userService.search(page, rows, $scope.searchEntity).success(
			function(response) {
				$scope.list = response.rows;
				$scope.paginationConf.totalItems = response.total; //更新总记录数
			}
		);
	}

	//上传图片并保存课程
	$scope.uploadAndSave = function() {
		//提交课程前先保存图片到数据库,并拿到相对路径
		//如果三个文件窗口都不等于null
		console.info("图片："+ $scope.entity.image1)
		if($scope.entity.image1 != null ) {
			userService.upload1().then(function(response1) {
				console.info($scope.entity);
				//保存图片后进行课程信息和图片路径的保存
				$scope.entity.code = response1.data.data.src;
						$scope.save();
					
				
			});
		} else{
			$scope.save();
		}
	}

	$scope.addImage1 = function() {
		angular.element('#imageShow1').click();
	}
	$scope.reloadList();
	//单个删除
	$scope.delsFlag = false;
	
	$scope.delsingle = function(id) {
		if(confirm('确定要删除这条数据吗?')) {
			userService.deleOne(id).success(
				function(response) {
					if(response.success) {
						alert(response.message);
						$scope.reloadList();
						$scope.delsFlag = true;
						$timeout(function() {
							$scope.delsFlag = false;
						}, 2000);
					}else{
						alert(response.message);
					}
				}
			);
		}
	};
	//编辑页面携带参数
	$scope.edit = function() {
		var url=$location.absUrl();
		var id=url.split("?id=")[1];
		$scope.findOne(id);
		console.log("the url is:"+url);
	}
});